package com.cgydu.service.impl;

import com.cgydu.dao.ProductDao;
import com.cgydu.dao.impl.ProductDaoImpl;
import com.cgydu.entity.PageBean;
import com.cgydu.entity.Product;
import com.cgydu.service.ProductService;

import java.sql.SQLException;
import java.util.List;

/**
 * projectName: day25_store
 * author: CGY
 * time: 2020/8/29 10:47
 * description:
 */
public class ProductServiceImpl implements ProductService {

    @Override
    public PageBean<Product> findAll(String tid, String currentPage, String pageSize) throws SQLException {

        //1.处理请求参数
        int cp = 1;
        if (currentPage != null) {
            cp = Integer.parseInt(currentPage);
        }

        int ps = 8;
        if (pageSize != null) {
            ps = Integer.parseInt(pageSize);
        }

        ProductDao productDao = new ProductDaoImpl();
        List<Product>  products =  productDao.queryProductByTid(tid,cp,ps);

        long totalCount = productDao.queryCountByTid(tid);

        //int currentPage, int pageSize, long totalCount, List<T> data
        PageBean<Product> pageBean = new PageBean<>(cp,ps,totalCount,products);
        return pageBean;
    }

    @Override
    public Product findProductByPid(String pid) throws SQLException {
        ProductDao productDao = new ProductDaoImpl();
        Product product = productDao.selectProductByPid(pid);

        return product;
    }
}